
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LPV_POS;
using LPV_POS.mh_pos;
using LPV_POS.Module;
public partial class frmDM_THE : Form
{
    private string ma = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    Boolean iloadform = false;
    string sDM_KhuyenMai_Ma = "";
    string sDM_NHOMKHACHHANG_MA = "";//FK_
    string sDM_KhuyenMaiThe_Ma = "";

    #region"    frmDM_THE"
    public frmDM_THE(string _sDM_KhuyenMai_Ma, string _sDM_KhuyenMai_Ten)
    {
        InitializeComponent();
        sDM_KhuyenMai_Ma = _sDM_KhuyenMai_Ma;
        lbel_tencongty.Text = lbel_tencongty.Text + " - " + _sDM_KhuyenMai_Ten;
    }
    #endregion

    
    #region"    frmDM_THE_Load"
    public void frmDM_THE_Load(object sender, EventArgs e)
    {
        btnluu.Enabled = false;
        btnxoa.Enabled = false;
        this.showcombobox();
        this.showdataDS();
        iloadform = true;

      
        this.gridControlRight_Click(null, null);
    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdata"
    void showdata()
    {

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMain = othuvien.SP_SelectDataTable("sp_DM_THE",
                           new string[] { "@flag", "@OBJID", "@MAKHUYENMAI" },
                           new object[] { 4, ma, sDM_KhuyenMai_Ma });

    }
    #endregion
    #region"    showdataDS"
    void showdataDS()
    {
        string[] param = new string[3];
        object[] value = new object[3];
        int ipos = 0;

        param[ipos] = "@flag";
        value[ipos++] = 4;

        param[ipos] = "@MAKHUYENMAI";
        value[ipos++] = sDM_KhuyenMai_Ma;

        param[ipos] = "@DM_NHOMKHACHHANG_MA";
        value[ipos++] = sDM_NHOMKHACHHANG_MA;

        //grdDM_THE.AutoGenerateColumns = false;

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMainDS = othuvien.SP_SelectDataTable("sp_DM_THE", param, value);
        grdDM_THE.DataSource = tbMainDS;
        grdDM_THE.Refresh();

    }
    #endregion
    #region"    showinfo"
    void showinfo(int i1edt2sch)
    {

        this.showdata();
        if (tbMain.Rows.Count == 0)
        {
            txtCODE.Text = "";

            if (cboDM_NHOMKHACHHANG_MA.Enabled && cboDM_NHOMKHACHHANG_MA.Items.Count > 0)
                cboDM_NHOMKHACHHANG_MA.SelectedIndex = 0;

            if (cboDM_KHACHHANG_MA.Enabled && cboDM_KHACHHANG_MA.Items.Count > 0)
                cboDM_KHACHHANG_MA.SelectedIndex = 0;

            return;
        }
        txtCODE.Text = tbMain.Rows[0]["CODE"] + "";
        cboDM_NHOMKHACHHANG_MA.SelectedValue = tbMain.Rows[0]["DM_NHOMKHACHHANG_MA"];
        DataTable tbDM_KHACHHANG = new thuvien().SP_SelectDataTable("sp_DM_THE",
                                                        new string[] { "@flag", "@DM_NHOMKHACHHANG_MA" },
                                                        new object[] { 5, tbMain.Rows[0]["DM_NHOMKHACHHANG_MA"] + "" });
        cboDM_KHACHHANG_MA.DataSource = tbDM_KHACHHANG;
        cboDM_KHACHHANG_MA.SelectedIndex = 0;
        cboDM_KHACHHANG_MA.SelectedValue = tbMain.Rows[0]["DM_KHACHHANG_MA"];

    }
    #endregion

    #region"    showcombobox"
    void showcombobox()
    {

        thuvien othuvien = new thuvien();


        DataTable tbDM_NHOMKHACHHANG = othuvien.SP_SelectDataTable("sp_DM_THE", new string[] { "@flag" }, new object[] { 6 });
        cboDM_NHOMKHACHHANG_MA.DataSource = tbDM_NHOMKHACHHANG;
        cboDM_NHOMKHACHHANG_MA.SelectedIndex = 0;

    }
    #endregion

    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
    #region"    btnDSthem_Click"
    private void btnDSthem_Click(object sender, EventArgs e)
    {
        iloadform = false;
        try
        {
            //thuvien othuvien = new thuvien();
            //string sSQL = "SELECT CODE = (IDENT_CURRENT('DM_THE') + IDENT_INCR('DM_THE'))";
            //DataTable tb = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            //int iCODE = 0;
            //if (tb != null)
            //    if (tb.Rows.Count > 0)
            //        int.TryParse(tb.Rows[0]["CODE"] + "", out iCODE);



            tbMain = tbMainDS.Copy();
            tbMain.Clear();
            ma = "-1";
            this.showinfo(1);
            this.setStatusButton(1);
            //txtCODE.Text = iCODE.ToString("N0");
        }
        catch (Exception ex)
        {
            string s = ex.Message;
        }
        iloadform = true;
    }

    #endregion
    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        //if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            this.Dispose();
        }

    }
    #endregion

    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "
    #region"    tao_ma_tu_dong"
    void tao_ma_tu_dong()
    {
    }

    #endregion
    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        if (this.save() > -1)
        {
            //sDM_KHACHHANG_MA = "";//FK_
            sDM_NHOMKHACHHANG_MA = "";//FK_
            //MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            MessageBox_LPV.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.showdataDS();

           // this.DialogResult = DialogResult.OK;

        }
    }

    #endregion

    #region"    save"
    int save()
    {
        int _iReasult = -1;
        if (this.ValidateSave())
        {
            string sCode = txtCODE.Text;
            if (txtCODE.Text == "")
                sCode = "0";

            thuvien othuvien = new thuvien();//lớp truy xuat du lieu
            _iReasult = othuvien.SP_Update("sp_DM_THE",
                 new string[] { "@flag", "@OBJID", "@CODE", "@DM_KHACHHANG_MA", "@DM_NHOMKHACHHANG_MA", "@MAKHUYENMAI" },
                 new object[] { 1, ma, txtCODE.Text, cboDM_KHACHHANG_MA.SelectedValue, cboDM_NHOMKHACHHANG_MA.SelectedValue, sDM_KhuyenMai_Ma });
        }
        return _iReasult;
    }

    #endregion

    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        if (cboDM_NHOMKHACHHANG_MA.SelectedIndex == 0)
        {
            new mh_thongbao("Chọn nhóm khách hàng", 2).ShowDialog();
            return false;
        }
        //else if (txtCODE.Text.Trim() == "")
        //{
        //    new mh_thongbao("Nhập thẻ", 2).ShowDialog();
        //    return false;
        //}
        //else if (ma.Trim() == "" || ma.Trim() == "-1")
        //{
        //    string sSQL = "SELECT 1 FROM DM_THE WHERE CODE LIKE '" + txtCODE.Text.Trim() + "'";
        //    DataTable tbDM_THE = new thuvien().SP_SelectDataTable("SP_EXECUTESQL",
        //                                                        new string[] { "@sSQL" },
        //                                                        new object[] { sSQL });
        //    if (tbDM_THE.Rows.Count > 0)
        //    {
        //        new mh_thongbao("Trùng thẻ", 2).ShowDialog();
        //        return false;
        //    }
        //}
        return true;
    }

    #endregion

    #region"    delete"
    int delete()
    {
        DataRow drMaThe = grvDM_THE.GetDataRow(grvDM_THE.FocusedRowHandle);
        sDM_KhuyenMaiThe_Ma = grvDM_THE.GetRowCellValue(grvDM_THE.FocusedRowHandle, col_IDKMT).ToString() + "";
        if (drMaThe != null)
        {
            thuvien othuvien = new thuvien();//lớp truy xuat du lieu    
            int _iReasult = othuvien.SP_Update("sp_DM_THE",
                                     new string[] { "@flag", "@MATHE", "@MAKHUYENMAI", "@DM_NHOMKHACHHANG_MA" ,"@OBJID"},
                                     new object[] { 3, drMaThe["CODE"], sDM_KhuyenMai_Ma, drMaThe["DM_NHOMKHACHHANG_MA"],sDM_KhuyenMaiThe_Ma });
            return _iReasult;
        }
        return 0;
    }

    #endregion

    #region"    btnxoa_Click"
    private void btnxoa_Click(object sender, EventArgs e)
    {
        if (LPV_POS.Module.MessageBox_LPV.Show("Bạn muốn xóa THẺ này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.OK)
        {
            if (this.delete() > 0)
            {
                LPV_POS.Module.MessageBox_LPV.Show("Xóa THẺ thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ma = "-1";
                //sDM_KHACHHANG_MA = "";//FK_
                sDM_NHOMKHACHHANG_MA = "";//FK_
                this.showdataDS();
                this.setStatusButton(4);
            }
        }
    }

    #endregion

    #region"    sự kiên chọn trên lưới -- tblDanhSach"
    #region"    showSearch"
    void showSearch(object sender, EventArgs e)
    {
        if (sender != null)
        {
            tbMain = (DataTable)sender;
            grdDM_THE.DataSource = tbMain;
            grdDM_THE.Refresh();
        }
    }
    #endregion
    #region"    frmDM_THE_KeyDown"
    private void frmDM_THE_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.T)
        {
            this.btnDSthem_Click(null, null);
        }
        else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.K)
        {
            //this.btnDStimkiem_Click(null, null);
        }
        else if (e.KeyCode == Keys.Escape)
        {
            this.btnDSthoat_Click(null, null);
        }
        else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.L)
        {
            this.btnluu_Click(null, null);
        }
        else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.X)
        {
            this.btnxoa_Click(null, null);
        }
    }
    #endregion
    #region"    grdDM_THE_SelectionChanged"
    //private void grdDM_THE_SelectionChanged(object sender, EventArgs e)
    //{


    //}
    private void grdDM_THE_DoubleClick(object sender, EventArgs e)
    {
        //this.grdDM_THE_SelectionChanged(null, null);
    }
    #endregion
    #endregion

    #region "    setStatusButton"
    void setStatusButton(int i1New2Edt4Del)
    {
        btnDSthem.Enabled = i1New2Edt4Del != 1;
        btnxoa.Enabled = (i1New2Edt4Del != 1 && i1New2Edt4Del != 4 && i1New2Edt4Del != 3);
        //btnDStimkiem.Enabled = i1New2Edt4Del != 3;
        btnluu.Enabled = i1New2Edt4Del != 5;
        txtCODE.ReadOnly = i1New2Edt4Del == 2;
    }
    #endregion

    #region "   txt_KeyDown "
    private void txt_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            this.cbo_SelectedIndexChanged(null, null);
    }
    #endregion

    #region "   cbo_SelectionChanged "
    private void cbo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            //sDM_KHACHHANG_MA = cboDM_KHACHHANG_MA.SelectedValue + "";
            sDM_NHOMKHACHHANG_MA = cboDM_NHOMKHACHHANG_MA.SelectedValue + "";

            DataTable tbDM_KHACHHANG = new thuvien().SP_SelectDataTable("sp_DM_THE",
                                                        new string[] { "@flag", "@DM_NHOMKHACHHANG_MA" },
                                                        new object[] { 5, sDM_NHOMKHACHHANG_MA });
            cboDM_KHACHHANG_MA.DataSource = tbDM_KHACHHANG;
            cboDM_KHACHHANG_MA.SelectedIndex = 0;
            //if (!btnDStimkiem.Enabled)
            //{
            iloadform = false;
            this.showdataDS();
            iloadform = true;
            //}
            //else
            //{
            //    sDM_KHACHHANG_MA = "";//FK_
            //    sDM_NHOMKHACHHANG_MA = "";//FK_

            //}
        }
    }
    #endregion

    #region"    grdDM_THE_DataError"
    private void grdDM_THE_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion

    #region"    grd_DataError"
    private void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion

    private void gridControlRight_Click(object sender, EventArgs e)
    {
        if (iloadform)
            if (grvDM_THE.FocusedRowHandle == 0 && grvDM_THE.RowCount == 1)
                this.gridViewRight_FocusedRowChanged(null, null);
    }
    #endregion

    private void lblDM_NHOMKHACHHANG_MA_Click(object sender, EventArgs e)
    {

    }

    private void bton_1_Click(object sender, EventArgs e)
    {
        mh_so omh_so = new mh_so();
        omh_so.txt_ketqua.Text = txtCODE.Text;
        omh_so.ShowDialog();
        if (omh_so.DialogResult == DialogResult.OK)
            txtCODE.Text = omh_so.txt_ketqua.Text;

    }

    private void gridViewRight_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
        if (iloadform)
        {
            if (grvDM_THE.FocusedRowHandle >= 0)
            {
                iloadform = false;
                ma = "-1";

                DataRow _dr = grvDM_THE.GetDataRow(grvDM_THE.FocusedRowHandle);
                if (_dr != null)
                {
                    ma = _dr["OBJID"] + "";//
                    this.setStatusButton(2);
                }
                this.showinfo(1);
                iloadform = true;
            }
        }
    }

    private void cboDM_KHACHHANG_MA_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (iloadform)
        {
            object o2011 = cboDM_KHACHHANG_MA.SelectedItem;
            if (o2011!=null)
            {
                DataRowView drv = (DataRowView)o2011;
                if (drv!=null)
                {
                    DataRow dr = drv.Row;
                    if (dr!=null)
                    {
                        string sMaThe = dr["CODE"]+"";
                        txtCODE.Text = sMaThe;
                    }
                }
            }
        }
    }

}

